package com.ruoyi.tcsp.service.impl;

import java.util.Arrays;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Optional;

import com.mongodb.client.result.UpdateResult;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.tcsp.domain.bean.req.TcspExpertInfoReq;
import com.ruoyi.tcsp.domain.mongo.TcspExpertOtherInfo;
import com.ruoyi.tcsp.domain.mysql.TcspExpertInfo;
import com.ruoyi.tcsp.repository.TcspExpertInfoRepository;
import com.ruoyi.tcsp.utils.ConvertUtils;
import com.ruoyi.tcsp.utils.MinioUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
import com.ruoyi.tcsp.mapper.TcspExpertInfoMapper;
import com.ruoyi.tcsp.service.ITcspExpertInfoService;


/**
 * 专家智库Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-04-15
 */
@Service
public class TcspExpertInfoServiceImpl implements ITcspExpertInfoService
{
    public static final Logger LOG = LoggerFactory.getLogger(TcspExpertInfoServiceImpl.class);
    @Autowired
    private TcspExpertInfoMapper tcspExpertInfoMapper;
    @Autowired
    private TcspExpertInfoRepository tcspExpertInfoRepository;
    @Autowired
    private MongoTemplate mongoTemplate;
    @Autowired
    private MinioUtils minioUtils;

    /**
     * 查询专家智库
     * 
     * @param expertId 专家智库主键
     * @return 专家智库
     */
    @Override
    public TcspExpertInfoReq selectTcspExpertInfoByExpertId(Long expertId)
    {
        TcspExpertInfoReq resp = new TcspExpertInfoReq();
        TcspExpertInfo baseInfo = tcspExpertInfoMapper.selectTcspExpertInfoByExpertId(expertId);
        TcspExpertOtherInfo otherInfo = null;
        try {
            otherInfo = tcspExpertInfoRepository.findById(String.valueOf(expertId)).get();
        } catch (NoSuchElementException e) {
            LOG.info("e: {}",e);
            LOG.info("没有其他信息存入，oterInfo赋为new TcspExpertOtherInfo();");
            otherInfo = new TcspExpertOtherInfo();
        }
        resp.setExpertName(baseInfo.getExpertName());
        resp.setExpertAvatar(minioUtils.getFileUrl(baseInfo.getExpertAvatar()));
        resp.setCollege(baseInfo.getCollege());
        resp.setJob(baseInfo.getJob());
        resp.setEmail(baseInfo.getEmail());
        resp.setSocialPartJob(otherInfo.getSocialPartJob());
        resp.setResearchDir(otherInfo.getResearchDir());
        resp.setEdbg(otherInfo.getEdbg());
        resp.setWorkExp(otherInfo.getWorkExp());
        resp.setArticle(otherInfo.getArticle());
        resp.setAwardInfo(otherInfo.getAwardInfo());
        resp.setTeachCour(otherInfo.getTeachCour());
        resp.setExpertId(baseInfo.getExpertId());
        resp.setPersonInfo(baseInfo.getPersonInfo());
        return resp;
    }

    /**
     * 查询专家智库列表
     * 
     * @param tcspExpertInfo 专家智库
     * @return 专家智库
     */
    @Override
    public List<TcspExpertInfo> selectTcspExpertInfoList(TcspExpertInfo tcspExpertInfo)
    {
        List<TcspExpertInfo> tcspExpertInfos = tcspExpertInfoMapper.selectTcspExpertInfoList(tcspExpertInfo);
        for (int i = 0; i < tcspExpertInfos.size(); i++) {
            tcspExpertInfos.get(i).setExpertAvatar(minioUtils.getFileUrl(tcspExpertInfos.get(i).getExpertAvatar()));
        }
        return tcspExpertInfos;
    }

    /**
     * 新增专家
     * @return 结果
     */
    @Override
    public int insertTcspExpertInfo(TcspExpertInfoReq req, LoginUser loginUser)
    {
        TcspExpertInfo tcspExpertInfo =  ConvertUtils.convert(req, loginUser);
        tcspExpertInfo.setCreateTime(DateUtils.getNowDate());
        TcspExpertOtherInfo otherInfo = new TcspExpertOtherInfo();
        int res = tcspExpertInfoMapper.insertTcspExpertInfo(tcspExpertInfo);
        otherInfo.setId(String.valueOf(tcspExpertInfo.getExpertId()));
        otherInfo.setAwardInfo(req.getAwardInfo());
        otherInfo.setEdbg(req.getEdbg());
        otherInfo.setArticle(req.getArticle());
        otherInfo.setResearchDir(req.getResearchDir());
        otherInfo.setTeachCour(req.getTeachCour());
        otherInfo.setWorkExp(req.getWorkExp());
        otherInfo.setSocialPartJob(req.getSocialPartJob());
        tcspExpertInfoRepository.save(otherInfo);
        return res;
    }

    /**
     * 修改专家智库
     * @return 结果
     */
    @Override
    public int updateTcspExpertInfo(TcspExpertInfoReq req, LoginUser loginUser)
    {

        TcspExpertInfo tcspExpertInfo =  ConvertUtils.convert(req, loginUser);
        tcspExpertInfo.setUpdateTime(DateUtils.getNowDate());
        tcspExpertInfo.setUpdateBy(loginUser.getUsername());
        tcspExpertInfo.setExpertId(req.getExpertId());
        tcspExpertInfo.setPersonInfo(req.getPersonInfo());
        TcspExpertOtherInfo otherInfo = new TcspExpertOtherInfo();
        int res = tcspExpertInfoMapper.updateTcspExpertInfo(tcspExpertInfo);
        Criteria criteria = new Criteria();
        criteria.and("id").is(String.valueOf(req.getExpertId()));
        Query query = new Query().addCriteria(criteria);
        Update update = new Update();
        if (req.getSocialPartJob() != null && !req.getSocialPartJob().isEmpty()) {
            update.set("social_partjob", req.getSocialPartJob());
        } else {
            update.set("social_partjob", "");
        }
        if (req.getResearchDir() != null && !req.getResearchDir().isEmpty()) {
            update.set("research_dir", req.getResearchDir());
        } else {
            update.set("research_dir", "");
        }
        if (req.getEdbg() != null && !req.getEdbg().isEmpty()) {
            update.set("edbg", req.getEdbg());
        } else {
            update.set("edbg", "");
        }
        if (req.getWorkExp() != null && !req.getWorkExp().isEmpty()){
            update.set("work_exp", req.getWorkExp());
        } else {
            update.set("work_exp", "");
        }
        if (req.getArticle() != null && !req.getArticle().isEmpty()) {
            update.set("article", req.getArticle());
        } else {
            update.set("article", "");
        }
        if (req.getTeachCour() != null && !req.getTeachCour().isEmpty()) {
            update.set("teach_cour", req.getTeachCour());
        } else {
            update.set("teach_cour", "");
        }
        if (req.getAwardInfo() != null && !req.getAwardInfo().isEmpty()) {
            update.set("award_info", req.getAwardInfo());
        } else {
            update.set("award_info", "");
        }
        UpdateResult updateResult = mongoTemplate.updateFirst(query, update, TcspExpertOtherInfo.class);
        return res;
    }

    /**
     * 批量删除专家智库
     * 
     * @param expertIds 需要删除的专家智库主键
     * @return 结果
     */
    @Override
    public int deleteTcspExpertInfoByExpertIds(Long[] expertIds)
    {
        Arrays.stream(expertIds).forEach(expertId -> {
            tcspExpertInfoRepository.deleteById(String.valueOf(expertId));
        });
        return tcspExpertInfoMapper.deleteTcspExpertInfoByExpertIds(expertIds);
    }

    /**
     * 删除专家智库信息
     * 
     * @param expertId 专家智库主键
     * @return 结果
     */
    @Override
    public int deleteTcspExpertInfoByExpertId(Long expertId)
    {
        tcspExpertInfoRepository.deleteById(String.valueOf(expertId));
        return tcspExpertInfoMapper.deleteTcspExpertInfoByExpertId(expertId);
    }
}
